package com.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiyang.entity.Building;
import com.yiyang.entity.Result;
import com.yiyang.mapper.BuildingMapper;
import com.yiyang.service.BuildingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    //@Autowired 都可以
    @Resource
    private BuildingMapper buildingMapper;

    @Override
    public Result<List<Building>> getBuilding(Integer currentPage, Integer pageSize, String name, String height) {
        //设置分页参数
        Page<Building> page = new Page<>(currentPage,pageSize);
        //设置查询条件参数
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name)
                .eq(StringUtils.isNotBlank(height),"height",height);
       this.buildingMapper.selectPage(page,wrapper);
        List<Building> list = page.getRecords();
        System.out.println(list);
        if (list.isEmpty()){
            return Result.fail();
        }else {
            System.out.println(page.getTotal());
            return Result.success(list,page.getTotal());
        }
    }

    @Override
    public Result<Void> deleteBuilding(String id) {
        int x = this.buildingMapper.deleteById(id);
        if(x== 1){
            return Result.success(null);
        }else{
            return Result.fail();
        }
    }

    @Override
    public Result<Void> addBuilding(Building building) {
        building.setCreateTime(new Date());
        building.setIsDelete(0);
        int insert = this.buildingMapper.insert(building);
        if(insert== 1){
            return Result.success(null);
        }else{
            return Result.fail();
        }
    }

    @Override
    public Result<Void> updateBuilding(Building building) {
        int update = this.buildingMapper.updateById(building);
        if(update== 1){
            return Result.success(null);
        }else{
            return Result.fail();
        }
    }
}
